import $ from 'jquery';
var u = navigator.userAgent.toLowerCase();
$(function () {
    // FastClick.attach(document.body);
    //反馈页面
    function all_disabled(val) {
        $('.f_back input').each(function () {
            $(this).attr('disabled', val);
        });
        $('.f_back button').each(function () {
            $(this).attr('disabled', val);
        });
    }

    function check_input() {
        var selected = $('li[data-select="1"]').length;
        var content = $('.f_back #content').val();

        if (selected || content != '') {
            $(".f_back #save_btn").css({"background": "#8EE2D3"});
            $(".f_back #save_btn").prop("disabled", false);
        } else {
            $(".f_back #save_btn").css({"background": "#AAA8A8"});
            $(".f_back #save_btn").prop("disabled", true);
        }
    }

    $('#content').on('input', function () {
        check_input();
    });
    $("#content").click(function () {
        $(this).css({color: "#333333"});
    })

    function save() {
        var arr = [];
        $('li[data-select="1"]').each(function () {
            arr.push($(this).find('span').html());
        });
        if ($('.f_back #content').val() != '') {
            arr.push($('.f_back #content').val());
        }
        if (!arr.length) {
            return;
        }
        var content = arr.join("\n");
        var uid = encodeURIComponent($('.f_back #uid').val());
        var version = encodeURIComponent($('.f_back #version').val());
        var model = encodeURIComponent($('.f_back #model').val());

        var url2 = document.referrer;
        all_disabled(true);
        $('.f_back #save_btn').html('正在提交，请稍候');
        $.ajax({
            type: "post",
            url: "/help/index/feedbackSave",
            dataType: "json",
            cache: false,
            timeout: 30000,
            data: {
                content: content,
                version: version,
                uid: uid,
                model: model
            },
            success: function (result) {
                if (result.status == 0) {
                    feedPopSuccess('提交成功');
                    $('.js_feed_btn').hide();
                    $('.js_feed_layer').height('5rem');
                    setTimeout(function () {
                        window.location.href = url2;
                    }, 1000)
                } else {
                    feedPopSuccess(result.errormsg);
                }
                all_disabled(false);
                $('.f_back #save_btn').html('点击反馈');
            },
            error: function () {
                feedPopSuccess('网络异常');
                all_disabled(false);
                $('.f_back #save_btn').html('点击反馈');
            }
        });
    }

    $(".f_back #content").css({"height": $(window).height() - $("#btm").height() - $("ul").height() - $("p").height() - 20 + "px"});
    $('.f_back #save_btn').on('click', save);

    $('.f_back li').each(function () {
        $(this).on('click', function () {
            $(this).find("img").show();
            var img = '';
            if ($(this).attr('data-select') == '0') {
                img = '../images/check.png';
                $(this).attr('data-select', '1');
            } else {
                $(this).find("img").hide();
                $(this).attr('data-select', '0');
            }
            $(this).find('img').attr('src', img);
            var counter = $('#content').val().length;
            if (counter > 400) {
                $(".f_back #save_btn").css({"background": "#AAA8A8"});
                $(".f_back #save_btn").prop("disabled", true);
            } else {
                check_input();
            }
        })
    });

    //反馈输入框字数限制
    $('#content').bind('propertychange input', function () {
        var counter = $('#content').val().length;
        $('#gulp_flexible .num').text(400 - counter);
        var num_l = -parseInt(400 - counter);
        if (counter > 400) {
            $('#test .num').text('已超出' + num_l + '个字');
            $(".f_back #save_btn").css({"background": "#AAA8A8"});
            $(".f_back #save_btn").prop("disabled", true);
        }
    });

    //获取客服
    $('.btm2,.a-click div').click(function () {
        kehu();
    })

    function kehu() {
        var can = getQueryString('cv');
        var can2 = can.replace(".", "");
        var can3 = can2.substr(2, 5);
        if (can.indexOf("Iphone") !== -1) {
            if (can3 >= 34.00) {
                $(".a-click").attr("href", "inke://pname=customerservice");
            } else {
                $(".a-click").attr("href", "http://kefu.easemob.com/webim/im.html?tenantId=13765");
            }
        } else if (can.indexOf("Android") !== -1) {
            if (can3 >= 32.00) {
                $(".a-click").attr("href", "inke://pname=customerservice");
            } else {
                $(".a-click").attr("href", "http://kefu.easemob.com/webim/im.html?tenantId=13765");
            }

        }
    }

    /*----cz.html-----*/
    var isiPad = /iPad/i.test(navigator.userAgent);
    var isiPhone = /iPhone|iPod/i.test(navigator.userAgent);
    var isAndroid = /Android/i.test(navigator.userAgent);
    var isIOS = (isiPad || isiPhone);
    if (isIOS) {
        $('.c_z .and').hide();
        $('.c_z .ios').show();
    } else {
        $('.ios').hide();
        $('.and').show();
    }

    /*----cz1.html-----*/
    if (isIOS) {
        $('.c_z1 .and').remove();
    } else {
        $('.c_z1 .ios').remove();
    }
    $("#main").css({"min-height": $(window).height() / 2 + "px"});
    /*----hot2.html-----*/
    $('.h_ot2 #main').css({'height': $(window).height() - $('#btm').height() + 'px'});
    if (isIOS) {
        $(".h_ot5 #main").css({"height": $(window).height() / 2 + "px"});
    } else {
        $(".h_ot5 #main").css({"height": "374px"});
    }
    $('.creat').click(function () {
        kehu();
    });
});
